package com.hddf.project.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import com.hddf.project.base.BaseDaoImpl;
import com.hddf.project.dao.PlanDao;
import com.hddf.project.pojos.ProjectPlan;
import com.hddf.project.pojos.ProjectPlanItem;
import com.hddf.project.util.O;

@SuppressWarnings("unchecked")
public class PlanDaoImpl extends BaseDaoImpl implements PlanDao {

	/**
	 * 根据计划ID查询
	 * 
	 * @param id
	 * @param s
	 * @return
	 * @throws Exception
	 */
	@Override
	public List<ProjectPlanItem> getPlanItemByPlanId(int id, Session s) throws Exception {
		Query query = s.createQuery("from ProjectPlanItem p where p.projectPlanId=:temp order by p.id asc");
		query.setInteger("temp", id);
		List<ProjectPlanItem> ppiList = query.list();
		return ppiList;
	}

	/**
	 * 获取所有计划
	 * 
	 * @return
	 * @throws Exception
	 */
	@Override
	public List<ProjectPlan> getAllPlan(Session s) throws Exception {
		Query query = s.createQuery("from ProjectPlan p order by p.inputTime desc");
		List<ProjectPlan> ppList = query.list();
		return ppList;
	}

	/**
	 * 根据计划名称和分页查询计划数据
	 * 
	 * @param s
	 * @return
	 * @throws Exception
	 */
	@Override
	public List<ProjectPlan> getPlanListByNameAndPage(String name, String startTime, String endTime,
			int startPage, int lengthData, Session s) throws Exception {
		StringBuffer sb = new StringBuffer();
		sb.append("from ProjectPlan p where 1 = 1");
		if (O.ckSt(name)) {
			sb.append(" and p.name like :ppn");
		}
		if (O.ckSt(startTime) && !O.ckSt(endTime)) {
			sb.append(" and p.inputTime >= :startTime ");
		} else if (!O.ckSt(startTime) && O.ckSt(endTime)) {
			sb.append(" and p.inputTime <= :endTime");
		} else if (O.ckSt(startTime) && O.ckSt(endTime)) {
			sb.append(" and p.inputTime between :startTime and :endTime ");
		}
		sb.append(" order by p.inputTime desc");
		Query query = s.createQuery(sb.toString());
		if (O.ckSt(name)) {
			query.setString("ppn", "%" + name + "%");
		}
		if (O.ckSt(startTime) && !O.ckSt(endTime)) {
			query.setString("startTime", startTime);
		} else if (!O.ckSt(startTime) && O.ckSt(endTime)) {
			query.setString("endTime", endTime);
		} else if (O.ckSt(startTime) && O.ckSt(endTime)) {
			query.setString("startTime", startTime);
			query.setString("endTime", endTime);
		}
		if (0 < lengthData) {
			query.setFirstResult(startPage);
			query.setMaxResults(lengthData);
		}
		List<ProjectPlan> ppList = query.list();
		return ppList;
	}

	/**
	 * 根据计划名称和分页查询计划数据长度
	 * 
	 * @param s
	 * @return
	 * @throws Exception
	 */
	@Override
	public int getPlansCountByNameAndPage(String name, String startTime, String endTime, Session s)
			throws Exception {
		StringBuffer sb = new StringBuffer();
		sb.append("select count(p.id) from ProjectPlan p where 1 = 1");
		if (O.ckSt(name)) {
			sb.append(" and p.name like :ppn");
		}
		if (O.ckSt(startTime) && !O.ckSt(endTime)) {
			sb.append(" and p.inputTime >= :startTime ");
		} else if (!O.ckSt(startTime) && O.ckSt(endTime)) {
			sb.append(" and p.inputTime <= :endTime");
		} else if (O.ckSt(startTime) && O.ckSt(endTime)) {
			sb.append(" and p.inputTime between :startTime and :endTime ");
		}
		Query query = s.createQuery(sb.toString());
		if (O.ckSt(name)) {
			query.setString("ppn", "%" + name + "%");
		}
		if (O.ckSt(startTime) && !O.ckSt(endTime)) {
			query.setString("startTime", startTime);
		} else if (!O.ckSt(startTime) && O.ckSt(endTime)) {
			query.setString("endTime", endTime);
		} else if (O.ckSt(startTime) && O.ckSt(endTime)) {
			query.setString("startTime", startTime);
			query.setString("endTime", endTime);
		}
		int result = 0;
		if (null != query.uniqueResult()) {
			result = Integer.parseInt(query.uniqueResult().toString());
		}
		return result;
	}

	/**
	 * 根据计划ID查询计划配置的数量
	 * 
	 * @param planId
	 * @return
	 * @throws Exception
	 */
	@Override
	public int getCountPlanItemByPlanId(int planId, Session s) throws Exception {
		Query query = s.createQuery("select count(p.id) from ProjectPlanItem p where p.projectPlanId =:planId");
		if (O.ckSt(planId)) {
			query.setInteger("planId", planId);
		}
		int result = 0;
		if (null != query.uniqueResult()) {
			result = Integer.parseInt(query.uniqueResult().toString());
		}
		return result;
	}

	/**
	 * 根据项目ID查询配置
	 * 
	 * @param projectId
	 * @param s
	 * @return
	 * @throws Exception
	 */
	@Override
	public List<ProjectPlanItem> getPlanItemByProjectId(int projectId, Session s) throws Exception {
		Query query = s.createQuery("from ProjectPlanItem p where p.projectId =:projectId order by p.id asc");
		query.setInteger("projectId", projectId);
		return query.list();
	}

	/**
	 * 根据项目ID和计划ID查询数量
	 * 
	 * @param projectId
	 * @param planId
	 * @param s
	 * @return
	 */
	@Override
	public int countPlanItem(int projectId, int planId, Session s) {
		Query query = s
				.createQuery("select count(p.id) from ProjectPlanItem p where p.projectPlanId =:ppid and p.projectId=:pid");
		query.setInteger("ppid", planId);
		query.setInteger("pid", projectId);
		if (null != query.uniqueResult()) {
			return Integer.parseInt(query.uniqueResult().toString());
		}
		return 0;
	}

	/**
	 * 根据项目ID查询数量
	 * 
	 * @param projectId
	 * @param planId
	 * @param s
	 * @return
	 */
	@Override
	public int countPlanItem(int projectId, Session s) {
		Query query = s.createQuery("select count(p.id) from ProjectPlanItem p where p.projectId=:pid");
		query.setInteger("pid", projectId);
		if (null != query.uniqueResult()) {
			return Integer.parseInt(query.uniqueResult().toString());
		}
		return 0;
	}
}